package com.karl.action;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.karl.entity.Product;
import com.karl.service.ProductManager;
import com.karl.vo.ProductInfo;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import jdk.nashorn.api.scripting.JSObject;

@Component("uploadProduct")
@Scope("prototype")
public class UploadProductAction extends ActionSupport implements ModelDriven<ProductInfo>{

	ProductManager productManager;
	List<Product> products;
	public List<Product> getProducts() {
		return products;
	}

	public void setProducts(List<Product> products) {
		this.products = products;
	}

	public ProductManager getProductManager() {
		return productManager;
	}

	@Resource
	public void setProductManager(ProductManager productManager) {
		this.productManager = productManager;
	}


	
	private ProductInfo productInfo = new ProductInfo();
	




	public String doAdd() {

		FileOutputStream fos = null;
		FileInputStream fis = null;
		try {
			
			fos = new FileOutputStream(productInfo.getSavaPath() + "/" + productInfo.getImageFileName());
			fis = new FileInputStream(productInfo.getImage());
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0)
				fos.write(buffer, 0, len);
			
			Product product = new Product();
			product.setProname(productInfo.getTitle());
			product.setProaddress(productInfo.getProaddress());
			product.setProprice(productInfo.getProprice());
			product.setImguri(productInfo.getImageFileName());
			productManager.add(product);
			
			

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (fis != null)
					fis.close();
				if (fos != null)
					fos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		return SUCCESS;
	}
	
	private String searchname;
	public String getSearchname() {
		return searchname;
	}

	public void setSearchname(String searchname) {
		this.searchname = searchname;
	}

	private int totalPage;
	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public String doList(){
		products = productManager.list(searchname);
		totalPage = productManager.count();
//		for (Iterator<Product> iter = products.iterator(); iter.hasNext();) {
//			  Product str = (Product)iter.next();
//			  System.out.println(str);
//		}
		return "list";
	}


	public String doDel(){
		products = productManager.list(searchname);
		return "del";
	}
	@Override
	public ProductInfo getModel() {
		// TODO Auto-generated method stub
		return productInfo;
	}


	public String doFirst(){
		products = productManager.firstPage().getList();
		totalPage = productManager.count();

		return "list";
	}
	public String doEnd(){
		products = productManager.endPage().getList();
		totalPage = productManager.count();

		return "list";
	}
	public String doPre(){
		products = productManager.prePage().getList();
		totalPage = productManager.count();

		return "list";
	}
	public String doNext(){
		products = productManager.nextPage().getList();
		totalPage = productManager.count();

		return "list";
	}
	

}
